#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<queue>
#include<vector>

using namespace std;

struct _greater
{
	bool operator()(const int n1, const int n2) {
		return n1 > n2;
	}
};

int main()
{
	int ret = 0;

	priority_queue<int,vector<int>,_greater> heap;
	int n, k;
	cin >> n;
	while (n--)
	{
		cin >> k;
		heap.push(k);
	}
	while(!heap.empty())
	{
		if (heap.size() == 1)
			break;
		int n1 = heap.top();
		heap.pop();
		int n2 = heap.top();
		heap.pop();
		ret += n1 + n2;
		heap.push(n1 + n2);
	}

	cout << ret;

	return 0;
}




